var setting = {
    showList: true,
    page: {
        pageNo: 10,
        total: 20,
        pageSize: 10,
        dataList: []
    },
    queryParam: {
        title: '',
    },
    text: {
        submitButtonText:'添加',
    },
    options: [],
    selectedData: [],
    form: {
        menu_id:'',
        title: '',
        type: 1,
        href: '',
        sort: '',
        icon:'',
        identify: '',
        super_id: ''
    },
    rules: {
        title: [
            { required: true, message: '请输入名称', trigger: 'blur' },
            { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' },
        ],
        sort: [
            { required: true, message: '排序不能为空'}
        ],
        identify: [
            { required: true, message: '权限标识不能为空'},
        ]
    },
    loading: false,
    typeEnum: [{text: '菜单', value: '1'},{text: '列表', value: '2'},{text: '按钮', value: '3'}],
    isAdd: true,
}

var vue = new Vue({
    el: '.main',
    data: setting,
    methods: {
        handleCurrentChange: function (pageNo) {//分页查询
            page(pageNo)
        },
        handleSizeChange: function (pageSize) {//修改pageSize查询
            vue.page.pageSize=pageSize
            page(1)
        },
        query: function () {//条件查询
            page(1)
        },
        getSelected: function (rows) {//获取选中内容
            vue.selectedData = rows;
        },
        changeAddOrEdit: function (type) {//切换添加
            if(type == 'add'){
                vue.text.submitButtonText = '添加';
                vue.isAdd = true;
                getSuperMenuList(1);
            }else {
                vue.text.submitButtonText = '修改';
                var selectedData = vue.selectedData;
                if(selectedData.length == 0){
                    vue.$message({
                        message: '请选择要修改的数据',
                        type: 'warning'
                    })
                    return ;
                }else if(selectedData.length > 1){
                    vue.$message({
                        message: '请选择一条要修改的数据',
                        type: 'warning'
                    })
                    return ;
                }else {
                    vue.isAdd = false;
                    $.post('/menu/getMenuById',{menu_id: selectedData[0].menu_id}, function (menu) {
                        vue.form = menu;
                    })
                }
            }
            vue.showList = false;
        },
        getChecked: function (value) {
            if(vue.form.type == 2 || vue.form.type == 3){
                var type = 1;
                if(vue.form.type == 2){
                    type = 1;
                }else{
                    type = 2;
                }
                getSuperMenuList(type);
            }else {
                vue.form.super_id = 0;
            }
        },
        resetForm: function () {
            this.$refs.form.resetFields();
        },
        goBack: function () {
          vue.showList = true;
          vue.resetForm();
        },
        addOrEdit: function () {
            vue.loading = true;
            vue.$refs['form'].validate(function (v) {
                if(v){
                    var data = vue.form;
                    data.super_id = data.super_id == ''?0:data.super_id;
                    var url = (vue.isAdd?'/menu/add':'/menu/edit');
                    data.menu_id = data.menu_id == ''?0:data.menu_id;
                    $.post(url, data, function (msg) {
                        vue.loading = false;
                        if(msg.status == 'SUCCESS'){
                            vue.$message({
                                message: msg.message,
                                type: 'success'
                            })
                            vue.showList = true;
                            page(vue.page.pageNo)
                            vue.resetForm();
                        }else{
                            vue.$message.error(msg.message);
                        }
                    })
                }
            })
        },
        del: function () {
            var data = vue.selectedData;
            if(data.length == 0){
                vue.$message({
                    message: '请选择要删除的数据',
                    type: 'warning'
                })
                return ;
            }
            this.$confirm('此操作将删除所选数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    url : "/menu/delete",
                    data : JSON.stringify(data),
                    type : "POST",
                    dataType : "json",
                    contentType :  "application/json;charset=utf-8",
                    success: function (msg) {
                        vue.$message({
                            message: msg.message,
                            type: 'success'
                        })
                        page(vue.page.pageNo)
                    }
                })
            }).catch(function () {
                vue.$message({
                    message: '已取消',
                    type: 'success'
                })
            })
        },
        filterType: function (value, row) {
            return row.type == value;
        }
    }
})
page(1)
function page(page) {
    var data = {
        page: page,
        pageSize: vue.page.pageSize,
        title: vue.queryParam.title,
    }
    $.post('/menu/queryByPage', data, function (page) {
        vue.page.dataList = page.rows;
        vue.page.pageNo = page.page;
        vue.page.total = page.total;
    })
}
function getSuperMenuList(type, menu_id) {
    $.post('/menu/getSuperList', {menu_id: menu_id,type: type}, function (options) {
        vue.options = options;
    })
}